package de.bensoft.jrail.client.com;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.log4j.Logger;

import de.bensoft.jrail.business.entity.Locomotive;
import de.bensoft.jrail.client.com.interfaces.LI100;

@Singleton
public class TrainCommunicator {
	
	@Inject
	LI100 li100;
	
	private static final Logger log = Logger.getLogger(TrainCommunicator.class);
	
	private List<TrainInstance> lstTrains;
	
	
	public TrainCommunicator() {
		lstTrains = new ArrayList<TrainInstance>();	
		log.info("Traincommunicator initialized!");
	}
	
	
	public TrainInstance getTrainInstanceByLocmotive(Locomotive locomotive) {
		TrainInstance ret = null;
		
		for(TrainInstance ti : lstTrains) {
			if(ti.getLocomotive().equals(locomotive))
				return ti;
		}
		
		// add a new Traininstance
		ret = new TrainInstance(this, locomotive);
		lstTrains.add(ret);
		
		return ret;
	}
	
	public void sendCommand(byte[] b) {
		li100.sendCommand(b);
	}
}
